package Cani;

import java.awt.Color;
import java.util.Stack;

import Desen.HighlightDesen;
import GUI.SimulareDesenPanel;

public class InAdancime {
	U1 u1 = new U1();
	U2 u2 = new U2();
	G1 g1 = new G1();
	G2 g2 = new G2();
	T12 t12 = new T12();
	T21 t21 = new T21();

	Operator op[] = { u1, u2, g1, g2, t12, t21 };
	Stack<Nod> nod_solutie = new Stack<Nod>();
	Stack<Class<? extends Operator>> operatori_use=new Stack<Class<? extends Operator>>();
	
	public void adaugare_prim_nod(Nod n) {
		nod_solutie.push(n);
	}

	public void rezolvare(Nod n) {
		Nod clona = new Nod(expandare(n));
		System.out.println("Prelucrare Clona:"+clona  );
		if(solutie(clona)){
			nod_solutie.push(clona);
			return;
		}
		else{
		     if (cauta_in_stiva_nod_solutie(clona) == false) { 
		    	  nod_solutie.push(clona);
		    	  for(int i=0;i<6;i++)
		    		  op[i].use=false;
		    	  rezolvare(clona);
		     }else {
		    	 operatori_use.pop();
		    	 rezolvare(n);}
			}
	}

	public void afisare_solutie() {
		int index=0;
		SimulareDesenPanel.textdesen.append("Din Cana1: "+ nod_solutie.elementAt(0).x +" Cana2: " + nod_solutie.elementAt(0).y +
				" -----> "+operatori_use.elementAt(index++)+ "-----> Cana1: "+ nod_solutie.elementAt(1).x+" Cana2: "+nod_solutie.elementAt(0).y+"\n\n");
		
		for(int i=2;i<nod_solutie.size()-1;i++){
			SimulareDesenPanel.textdesen.append("Din Cana1: "+ nod_solutie.elementAt(i-1).x +" Cana2: " + nod_solutie.elementAt(i-1).y +
					" -----> "+operatori_use.elementAt(index)+ "-----> Cana1: "+ nod_solutie.elementAt(i).x+" Cana2: "+nod_solutie.elementAt(i).y+"\n\n");
			index++;
		}
		
		SimulareDesenPanel.textdesen.append("Din Cana1: "+ nod_solutie.elementAt(nod_solutie.size()-2).x +" Cana2: " + nod_solutie.elementAt(nod_solutie.size()-2).y +
				" -----> "+operatori_use.elementAt(index++)+ "-----> Cana1: "+ nod_solutie.elementAt(nod_solutie.size()-1).x+" Cana2: "+nod_solutie.elementAt(nod_solutie.size()-1).y+"\n\n");
		String finalSol="Cana2: "+nod_solutie.elementAt(nod_solutie.size()-1).y+"";
		HighlightDesen.drawHighlight(finalSol, 10, Color.green);

	}

	public boolean cauta_in_stiva_nod_solutie(Nod n) {
		for (int i = 0; i < nod_solutie.size(); i++)
			if ((nod_solutie.get(i).x == n.x) && (nod_solutie.get(i).y == n.y))
				return true;
		return false;
	}

	public Nod expandare(Nod n) {
		Nod clona = new Nod(n);
		for (int i = 0; i < 6; i++) {
			if (op[i].condition(clona) && (op[i].use == false)) {
				clona = new Nod(op[i].action(clona));
				op[i].use = true;
				operatori_use.push(op[i].getClass());
				return clona;
			}
		}
		return clona;
	}

	public boolean solutie(Nod n) {
		if (n.getY() == 2)
			return true;
		return false;

	}

	public Stack<Nod> getNod_solutie() {
		return nod_solutie;
	}

//	public static void main(String args[]) {
//		InAdancime larg = new InAdancime();
//		Nod n = new Nod(0, 0);
//		larg.adaugare_prim_nod(n);
//		larg.rezolvare(n);
//		larg.afisare_solutie();
//
//	}
}
